package cn.favebook.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;

import cn.favebook.Global;
import cn.favebook.db.UserDao;

/**
 * Servlet implementation class CheckData
 */
@WebServlet("/CheckData")
public class CheckData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");

//		String email = request.getParameter("email");

		response.setContentType("application/json; charset=utf8");
		PrintWriter out = response.getWriter();
		
		if (username == "") {
			out.println("{\"ok\": null}");
		}else {
		
			QueryRunner runner = (QueryRunner) request.getServletContext().getAttribute(Global.KEY_QUERY_RUNNER);
			boolean userIsExists = false;
	//		boolean emailIsExists = false;
			try {
				userIsExists = UserDao.unameExists(runner, username);
	//			emailIsExists = UserDao.emailExists(runner, email);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			
			if (userIsExists) {
				out.println("{\"ok\": 0}");
			} else {
				out.println("{\"ok\": 1}");
			}
		
//		if (emailIsExists) {
//			out.println("{\"OK\": 2}");
//		} else {
//			out.println("{\"OK\": 0}");
//		}
		}
	}

}
